const { fetch } = require('../../utils/util.js')
const app = getApp()
Page({
  data: {
    pageIndex: 0,
    pageSize: 10,
    totalCount: 0,
    hasMore: true,
    options: [], //筛选项
    list: [], // 工地列表
    params: {},
    activeIndex: null,
    itemIndex: null,
    show: false,
    isScroll:true
  },

  onLoad: function (options) {
    this.getOptions(options)
  },
  // 获取筛选项
  getOptions: function (options = {}) {
    const _this = this;
    // 选项
    return fetch({
      url: 'api/zhuang/GetDicList',
      params: {},
      success: (res) => {
        console.log('字典：')
        console.log(res)
        if (options.area) {
          res[0]['data'].map(item => {
            if (item.id == options.area) {
              res[0]['typeName'] = item.value
            }
          })
          _this.setData({
            options: res,
            params: { area: parseInt(options.area) }
          }, () => {
            _this.loadmore()
          })
        } else {
          _this.setData({
            options: res,
          }, () => {
            _this.loadmore()
          })
        }
      }
    });
  },

  onPullDownRefresh: function () {
    this.setData({ pageIndex: 0, hasMore: true, params: {} }, () => {
      this.getOptions().then(() => wx.stopPullDownRefresh())
    })
  },

  /**
  * 页面上拉触底事件的处理函数--加载数据
  */
  onReachBottom: function () {
    this.loadmore()
  },

  // 加载更多
  loadmore: function () {
    const _this = this;
    if (!this.data.hasMore) return;
    let { pageIndex, pageSize, params, list } = this.data;
    const obj = { ...params, page: ++pageIndex, rows: pageSize }
    // 选项
    fetch({
      url: 'api/zhuang/GetGonDiList',
      params: obj,
      success: (res) => {
        console.log('案例地图：')
        console.log(res)
        if (res && res.items) {
          const totalCount = parseInt(res.totalItems)
          const hasMore = pageIndex * pageSize < totalCount

          let tempList = null;
          if (pageIndex == 1) {
            tempList = res.items
          } else {
            tempList = list.concat(res.items)
          }
          this.setData({ totalCount, pageIndex, hasMore, list: tempList })
        }

      }
    })
  },

  linkpage: function (e) {
    app.pageTo(e.currentTarget.dataset.url)
  },

  // 下了筛选
  selectChange: function (e) {
    this.setData({
      activeIndex: e.currentTarget.dataset.index,
      show: true,
      isScroll:false
    })
  },
  
  // 关闭弹窗
  close: function () {
    this.setData({
      activeIndex: null,
      show: false,
      isScroll: true
    })
  },

  // 筛选
  selectClick: function (e) {
    let index = e.currentTarget.dataset.index
    let { activeIndex, options, params } = this.data;
    const _this = this;
    if (activeIndex == 0) {
      params.area = options[activeIndex]['data'][index]['id']
    } else {
      params.type = options[activeIndex]['data'][index]['id']
    }
    options[activeIndex]['typeName'] = options[activeIndex]['data'][index]['value']
    _this.setData({
      options,
      params,
      hasMore: true,
      pageIndex: 0,
      pageSize: 10,
      totalCount: 0,
    }, () => {
      _this.loadmore()
      _this.close()
    })

  }
})